package com.silverhaze.diamond.client.ui.dialogs;

import com.silverhaze.commons.helpers.CommonUIHelper;
import com.silverhaze.diamond.common.entity.Customer;
import com.silverhaze.diamond.client.data.SpecificData;
import com.silverhaze.diamond.client.ui.UIHelper;
import com.silverhaze.diamond.client.ui.table.CustomersTable;
import com.silverhaze.diamond.client.ui.table.model.CustomersTableModel;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.*;

/**
 * Created by Ivan Babanin (16:16:42 02.06.2008)
 * This part of code is strictly confidential.
 * © Magnificent Software Inc.
 */
public class CustomerSelectorDialog extends AbstractDialog {

    private Customer selectedCustomer;
    private DialogConnector dialogConnector;

    public CustomerSelectorDialog(Frame owner, DialogConnector dialogConnector) {
        super(owner);
        this.dialogConnector = dialogConnector;

        initView();
        setTitle(getCaption() + "Выбор покупателя");
        setLocationRelativeTo(null);
    }

    private void initView() {

        final CustomersTableModel dm = new CustomersTableModel();
        final CustomersTable userSelector = new CustomersTable(dm);
        userSelector.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                int selectedRow = userSelector.getSelectedRow();
                if (selectedRow >= 0) {
                    selectedCustomer = dm.getObjectAtRow(selectedRow);
                }
            }
        });

        userSelector.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                if (e.getClickCount() >= 2) {
                    if (selectedCustomer != null) {
                        okPressed();
                    }
                }
            }
        });

        final JTextField nameField = new JTextField("*");
        nameField.addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent e) {
                ((CustomersTableModel) userSelector.getModel()).fillData(SpecificData.filterCustomers(nameField.getText().trim()));
                ((CustomersTableModel) userSelector.getModel()).refreshModel();
            }
        });

        JPanel tablePanel = CommonUIHelper.packInPanel(CommonUIHelper.createVerticalBox(new JScrollPane(userSelector), 3,
                CommonUIHelper.createHorizontalBox(
                        2, "Имя: ", nameField, 2
                ))
        );
        tablePanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        JPanel btnPanel = new JPanel(new BorderLayout());
        btnPanel.add(CommonUIHelper.createHorizontalBox(
                new JButton(new AbstractAction("Выбрать") {
                    public void actionPerformed(ActionEvent e) {
                        okPressed();
                    }
                }),
                1,
                new JButton(new AbstractAction("Отмена") {
                    public void actionPerformed(ActionEvent e) {
                        cancelPressed();
                    }
                })

        ), BorderLayout.LINE_END);

        btnPanel.add(new JButton(new AbstractAction("Добавить покупателя") {
            public void actionPerformed(ActionEvent e) {
                CustomerDialog.createCustomer(CustomerSelectorDialog.this, new DialogConnector() {
                    public void objectSelected(Object o) {
                        dialogConnector.objectSelected(o);
                        CustomerSelectorDialog.this.dispose();
                    }
                });
            }
        }), BorderLayout.LINE_START);
        btnPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));


        add(tablePanel, BorderLayout.CENTER);
        add(btnPanel, BorderLayout.PAGE_END);

        pack();
    }

    protected void okPressed() {
        dialogConnector.objectSelected(selectedCustomer);
        dispose();
    }

    protected void cancelPressed() {
        dispose();
    }

    public static void selectCustomer(DialogConnector dialogConnector) {
        CustomerSelectorDialog userSelectorDialog = new CustomerSelectorDialog(UIHelper.getMainFrame(), dialogConnector);
        userSelectorDialog.setVisible(true);
    }
}

